import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:meal/constant/antd_icons.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/model/biz_time_range.dart';
import 'package:meal/ui/delivery_period_pricker.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:meal/utils/tools.dart';

///
/// 营业时段
///
class BizTime extends StatefulWidget {
  const BizTime({Key? key}) : super(key: key);

  @override
  State<BizTime> createState() => _BizTimeState();
}

class _BizTimeState extends State<BizTime> {
  List<BizTimeRange> times = [];

  Widget get actions {
    return BrnTextAction(
      '确定',
      iconPressed: onSure,
      themeData: BrnAppBarConfig(
          actionsStyle: BrnTextStyle(
              color: Colors.white, fontSize: Sizes.default16, fontWeight: FontWeight.normal)),
    );
  }

  void onSure() {
    Get.back();
  }

  @override
  void initState() {
    times.add(BizTimeRange(start: '11:30', end: '15:30'));
    times.add(BizTimeRange(start: '16:30', end: '22:30'));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ToolsUtils.getDefaultBackgroundColor(),
      appBar: ToolsUtils.getDefaultBrnAppBar('营业时段', actions: actions),
      body: SingleChildScrollView(
        child: Container(
          decoration: const BoxDecoration(
            color: Colors.white,
          ),
          padding:
              const EdgeInsets.symmetric(vertical: Sizes.default10, horizontal: Sizes.default15),
          child: Column(
            children: [
              ListBody(
                  children: times.map((e) {
                return SizedBox(
                  height: Sizes.default50,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text('${e.start}-${e.end}',
                          style: StylesUtils.customTextStyle(
                              fontSize: Sizes.default15, color: ColorsUtils.color333)),
                      IconButton(
                          onPressed: () {
                            setState(() {
                              times.remove(e);
                            });
                          },
                          icon: Icon(
                            AntdIcons.close,
                            size: Sizes.default20,
                            color: ColorsUtils.parseColorFromHexString(ColorsUtils.color666),
                          ))
                    ],
                  ),
                );
              }).toList()),
              Container(
                  alignment: Alignment.center,
                  child: TextButton(
                    onPressed: () {
                      showBottom(context);
                    },
                    child: Text('添加时段 ${times.length}/5',
                        style: StylesUtils.customTextStyle(
                            fontSize: Sizes.default15, color: ColorsUtils.colorFf6)),
                  ))
            ],
          ),
        ),
      ),
    );
  }

  showBottom(BuildContext context) {
    ///
    /// isScrollControlled 是否时全屏还是半屏
    /// isDismissible 外部是否可以点击，false不可以点击，true可以点击，点击后消失
    /// backgroundColor 通常可以设置白色和透明，
    /// barrierColor 设置遮挡底部的半透明颜色,默认是black54,可以设置成透明的;
    /// enableDrag 是否可以向下拖动关闭,默认是true打开的;
    ///
    showModalBottomSheet(
        context: context,
        elevation: 0,
        isScrollControlled: false,
        useRootNavigator: true,
        backgroundColor: Colors.transparent,
        builder: (BuildContext context) {
          return DeliveryPeriodPricker(onConfirm: (String start, String end) {
            if (start.isNotEmpty && end.isNotEmpty) {
              setState(() {
                times.add(BizTimeRange(start: start, end: end));
              });
            }
          });
        });
  }
}
